<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2020 All rights reserved.
// +----------------------------------------------------------------------
// | User: Simony <1362229026@qq.com>
// | wechat: Shen86579595
// +----------------------------------------------------------------------

namespace app\common\logic\evaluate;

use \think\Db;

class EvaluateLogLogic
{
    /**
     * User:Simony
     * Function:  评价
     * @param $evaluate_member_id 评价用户
     * @param $been_evaluate_member_id 被评价用户
     * @param $types
     * @return array
     */
    public function evaluate($evaluate_member_id, $been_evaluate_member_id, $types)
    {
        if ($evaluate_member_id == $been_evaluate_member_id) return ds_arr(ERROR, '自己不能评论自己');

        $memberModel = model('common/member/Member');
        $evaluateLogModel = model('common/evaluate/EvaluateLog');
        $evaluateTypeModel = model('common/evaluate/EvaluateType');

        $evaluate_member = $memberModel->getInfoByID($evaluate_member_id);
        if (!$evaluate_member) return ds_arr(ERROR, '评价用户不存在');

        $member = $memberModel->getInfoByID($been_evaluate_member_id);
        if (!$member) return ds_arr(ERROR, '被评价用户不存在');

        $type_arr = explode(',', $types);
        $type_list = $evaluateTypeModel->getCol('id,name', ['id' => $type_arr, 'type' => [3, $member['sex']]]); //过滤掉非这个性别使用的标签
        if (!$type_list) return ds_arr(ERROR, '评价类型也不存在');
        if ($evaluateLogModel->isEvaluate($evaluate_member_id, $been_evaluate_member_id)) return ds_arr(ERROR, '已评价过');

        $authLogic = model('common/AuthLogic', 'logic');
        $is_expire = $authLogic->isExpire($evaluate_member_id, $been_evaluate_member_id, 2); //社交信息
        if ($is_expire == true) return ds_arr(ERROR, '未获取联系方式');

        $evaluate_list = $evaluateLogModel->getList(['type' => $type_arr, 'been_member_id' => $been_evaluate_member_id], 0, 'id,type,evaluate_member_ids');
        $update_data = [];
        foreach ($evaluate_list as $v) {
            $update_data [] = [
                'id' => $v['id'],
                'type' => $v['type'],
                'evaluate_member_ids' => $v['evaluate_member_ids'] ? $v['evaluate_member_ids'] . ',' . $evaluate_member_id : $evaluate_member_id,
            ];
            unset($type_list[$v['type']]); //删除更新的
        }

        $add_data = [];
        foreach ($type_list as $type_id => $type_name) {
            $add_data[] = [
                'type' => $type_id,
                'been_member_id' => $been_evaluate_member_id,
                'evaluate_member_ids' => $evaluate_member_id,
            ];
        }

        $update_res = $update_data ? $evaluateLogModel->saveAll($update_data) : true;
        if (!$update_res) return ds_arr(ERROR, '操作失败1');

        $add_res = $add_data ? $evaluateLogModel->insertAll($add_data) : true;
        if (!$add_res) return ds_arr(ERROR, '操作失败2');

        return ds_arr(SUCCESS, '操作成功');
    }
}
